package com.aistar.util;

import java.util.Random;

public class MathUtil {
    /**
     * 获得不重复随机数
     * <p>
     *     如果
     *     <code>count > maxNum - minNum</code>
     *     则获取到的数组大小 = maxNum - minNum
     * </p>
     *
     * @param count  需要生成的随机数的个数
     * @param minNum 随机数最小值
     * @param maxNum 随机数最大值
     */
    public static int[] getRandomArray(int count, int minNum, int maxNum) {
        int[] output = new int[
                count <= maxNum - minNum ? count : maxNum - minNum
                ];
        Random random = new Random();
        int index = 0;
        while (index < output.length) {
            int temp = random.nextInt(maxNum - minNum) + minNum;
            boolean flag = false;
            for (int i = 0; i < index; i++) {
                if (output[i] == temp)
                    flag = true;
            }
            if (!flag)
                output[index++] = temp;
        }
        return output;
    }
}
